package com.csglxt.view.action;

import java.io.PrintWriter;
import java.math.BigDecimal;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.csglxt.base.BaseAction;
import com.csglxt.domain.Order;
import com.csglxt.domain.User;

/**
 * 
 * @ClassName: ReportAction
 * @Description: 报表控制器
 * @author Delver_Si
 * @date 2015-5-28 下午11:59:25
 * 
 */

@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class ReportAction extends BaseAction<Order> {
    private String year;

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    /**
     * Ajax请求获取当年每月进货消费数据
     * 
     * @throws Exception
     */
    public void ajaxReturnGoodsReport() throws Exception {
        BigDecimal[] reportArray = new BigDecimal[13];
        BigDecimal maxRreport = new BigDecimal(1);// 当年每月最大进货消费
        for (int i = 1; i <= 12; i++) {
            BigDecimal report = orderService.queryByYearAndMonth(
                    Integer.parseInt(year), i);
            if (report != null) {
                if (report.compareTo(maxRreport) > 0) {
                    maxRreport = report;
                }
            } else if (report == null) {
                report = new BigDecimal(0);
            }
            reportArray[i - 1] = report;
        }

        StringBuilder reportJson = new StringBuilder();
        reportJson.append("{");
        for (int i = 0; i < reportArray.length - 1; i++) {

            reportJson.append("\"m" + (i + 1) + "\":" + reportArray[i] + ",");

        }
        reportJson.append("\"max" + "\":" + maxRreport);
        reportJson.append("}");
        // System.out.println("sss " + maxRreport);
        // System.out.println(reportJson);

        // System.out.println(Arrays.toString(reportArray));
        PrintWriter pw = ServletActionContext.getResponse().getWriter();
        // pw.print(Arrays.toString(reportArray));
        pw.print(reportJson);
        pw.flush();
        pw.close();
    }

    /**
     * 进货消费统计
     * 
     * @return
     */
    public String returnGoodsUI() {
        if (!((User) getSession().get("user")).getRole().equals("1")) {
            return "noPrivilege";
        } else {
            /*
             * List reportList = new LinkedList(); BigDecimal maxRreport = new
             * BigDecimal(1);// 当年每月最大进货消费 for (int i = 1; i <= 12; i++) {
             * if(year==null){
             * 
             * year
             * =String.valueOf((Integer.parseInt(DateUtil.getCursqlDate().toString
             * ().split("-")[0]) -1)); }
             * 
             * BigDecimal report =
             * orderService.queryByYearAndMonth(Integer.parseInt(year), i); if
             * (report != null) { if (report.compareTo(maxRreport) > 0) {
             * maxRreport = report; } }else if (report==null) { report=new
             * BigDecimal(0); } reportList.add(report); }
             * //System.out.println("cc " + reportList.get(1));
             * ActionContext.getContext().put("reportList", reportList);
             * ActionContext.getContext().put("maxRreport", maxRreport);
             * ActionContext.getContext().put("year", year);
             */
            return "returnGoods";
        }
    }
}
